package blackitheima1.Test;

import blackitheima1.Domain.Student;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class XMLTest {
    public static void main(String[] args) throws DocumentException {
        //创建解析器
        SAXReader saxReader=new SAXReader();
        //读取xml文件,返回一个文档对象
        Document document = saxReader.read(new File("day17\\src\\blackitheima1\\xml\\Xml.xml"));

        //调用方法获取根标签
        Element rootElement = document.getRootElement();
        //调用方法获取子标签
        List<Element> elements = rootElement.elements("student");

        //创建集合封装数据
        ArrayList<Student> list=new ArrayList<>();

        //遍历获得的子标签中的对象
        for (Element element : elements) {
            //调用方法获取属性值id;
            Attribute idelement = element.attribute("id");
            String id = idelement.getValue();

            //获取子标签其他成员变量值
            Element element1 = element.element("name");
            String name = element1.getText();

            Element element2 = element.element("age");
            String age = element2.getText();

            Element element3 = element.element("info");
            String info = element3.getText();

            Element element4 = element.element("manage");
            String manage = element4.getText();

            //创建对象封装
            Student stu=new Student(Integer.parseInt(id),name,Integer.parseInt(age),info,manage);

            //将对象添加到集合中
            list.add(stu);
        }
        //遍历集合
        for (Student student : list) {
            System.out.println(student);
        }
    }
}
